package services;

import entities.Book;

import java.util.ArrayList;
import java.util.List;
public class LibraryService {
    private List<Book> books = new ArrayList<>();

    public LibraryService(){
        books.add(new Book("三国演义", "罗贯中", 99.0, "历史"));
        books.add(new Book("西游记", "吴承恩", 88.0, "神话"));
        books.add(new Book("红楼梦", "曹雪芹", 77.0, "小说"));
        books.add(new Book("水浒传", "施耐庵", 66.0, "历史"));
    }
    //方法实现
    //获取图书
    public List<Book> getAllBooks(){
        return books;
    }
    //查找图书
    public Book findBook(String title){
        for(Book book : books){
            if(book.getTitle().equals(title)){
                return book;
            }
        }
        return null;
    }
    //添加图书
    public void addBook(Book book){
        books.add(book);
        System.out.println("已添加图书：" + book);
    }
    //删除图书
    public void removeBook(String title){
        Book bookToRemove = findBook(title);
        if(bookToRemove != null){
            books.remove(bookToRemove);
            System.out.println("图书已删除：" + bookToRemove);
        }else{
            System.out.println("未找到该书：" + title);
        }
    }
    //借阅图书
    public void borrowBook(String title){
        Book bookToBorrow = findBook(title);
        if(bookToBorrow == null){
            System.out.println("未找到该书：" + title);
            return;
        }

        if( bookToBorrow.isBorrowed()){
            System.out.println("该书已被借阅：" + title);
            return;
        }

        bookToBorrow.setBorrowed(true);
        System.out.println("您已成功借阅：" + bookToBorrow.getTitle());
    }
    //归还图书
    public void returnBook(String title){
        Book bookToReturn = findBook(title);
        if(bookToReturn == null){
            System.out.println("未找到该书：" + title);
            return;
        }
        if(!bookToReturn.isBorrowed()){
            System.out.println("该书显示在库，请重新输入：" + title);
            return;
        }

        bookToReturn.setBorrowed(false);
        System.out.println("您已成功归还：" + bookToReturn.getTitle());
    }
}
